package com.example.aicloud.controller;


import com.xxl.job.core.handler.annotation.XxlJob;
import jakarta.annotation.Resource;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * kafka 调用测试控制器
 */
@RestController
@RequestMapping("/kafka")
public class KafkaController {

    @Resource
    private KafkaTemplate kafkaTemplate;
    private static final String TOPIC = "ai-cloud-test-topic";

    @RequestMapping("/send")
    public String send(String msg) {
        if (!StringUtils.hasLength(msg)) {
            return "请先输入发送的消息！";
        }
        kafkaTemplate.send(TOPIC, msg);
        return "ok";
    }

    @KafkaListener(topics = {TOPIC})
    public void listen(String msg,Acknowledgment ack) {
        System.out.println("接收到消息：" + msg);
        // 手动确认消息
        ack.acknowledge();
    }

    @XxlJob("testJob")
    public void testJob() {
        System.out.println("执行了定时任务");
    }



}
